/*
 * ========================================================
 * Copyright(c) 2014 杭州龙骞科技-版权所有
 * ========================================================
 * 本软件由杭州龙骞科技所有, 未经书面许可, 任何单位和个人不得以
 * 任何形式复制代码的部分或全部, 并以任何形式传播。
 * 公司网址
 * http://www.hzdracom.com/
 * ========================================================
 */
package com.shwread.android.qysw.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckedTextView;

import com.netease.nimlib.sdk.team.model.Team;
import com.shwread.android.qysw10000204.R;

import java.util.HashMap;
import java.util.List;


/**
 * @Project： qysw_1.0.8_dev
 * @Title：SelectAdapter
 * @Description：
 * @Author：wayne
 * @Date：2015年5月20日下午4:56:04
 */
public class SelectAdapter
        extends
        BaseAdapter
{
	
	// 填充数据的list  
	private List<Team>                list;
	// 用来控制CheckBox的选中状况  
	private static HashMap<Integer, Boolean> isSelected;
	// 上下文  
	private Context                          context;
	// 用来导入布局  
	private LayoutInflater                   inflater = null;
	
	// 构造器  
	public SelectAdapter(List<Team> list, Context context) {
		this.context = context;
		this.list = list;
		inflater = LayoutInflater.from(context);
		isSelected = new HashMap<Integer, Boolean>();
		// 初始化数据  
		initDate();
	}
	
	// 初始化isSelected的数据  
	private void initDate() {
		for (int i = 0; i < list.size(); i++)
		{
			getIsSelected().put(i, false);
		}
	}
	
	@Override
	public int getCount() {
		return list.size();
	}
	
	@Override
	public Object getItem(int position) {
		return list.get(position);
	}
	
	@Override
	public long getItemId(int position) {
		return position;
	}
	
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		SelectViewHolder holder = null;
		if (convertView == null)
		{
			// 获得ViewHolder对象  
			holder = new SelectViewHolder();
			// 导入布局并赋值给convertview  
			convertView = inflater.inflate(R.layout.select_group_item, null);
			holder.ctv = (CheckedTextView) convertView.findViewById(R.id.item_cb);
			// 为view设置标签  
			convertView.setTag(holder);
		}
		else
		{
			// 取出holder  
			holder = (SelectViewHolder) convertView.getTag();
		}
		
		// 设置list中TextView的显示  
		holder.ctv.setText(list.get(position).getName());
		// 根据isSelected来设置checkbox的选中状况  
		holder.ctv.setChecked(getIsSelected().get(position));
		return convertView;
	}
	
	public static HashMap<Integer, Boolean> getIsSelected() {
		return isSelected;
	}
	
	public static void setIsSelected(HashMap<Integer, Boolean> isSelected) {
		SelectAdapter.isSelected = isSelected;
	}
	
	public class SelectViewHolder
	{
		public CheckedTextView ctv;
	}
}
